import functools

from flask import current_app
from flask import g
from flask import session


def do_index_class(index):
    """自定义过滤器"""
    if index == 1:
        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""


def user_login_data(view_func):
    """查询当前客户端登录用户信息的装饰器"""
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id")
        from info.models import User
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        # 通过g变量，保存用户信息，后面提供给视图函数使用，视图中可以通过g.user来获取数据
        g.user = user
        return view_func(*args, **kwargs)
    return wrapper
